package cn.sdhuijin.healthexam.control;

import java.util.Properties;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import cn.sdhuijin.healthexam.util.Constants;

public class RadioEditUnionViewControl implements IControl,
		OnCheckedChangeListener, OnClickListener {

	private Activity activity;
	private int[] editTextIds;
	private String name;
	private String[] editTextNames;
	private RadioGroup radioGroup;
	private String enableStr;
	private int radioId;

	public RadioEditUnionViewControl(Activity activity, int[] editTextIds,
			String name, String[] editTextNames, int radioGroupId,
			String enableStr) {
		this.activity = activity;
		this.editTextIds = editTextIds;
		this.name = name;
		this.editTextNames = editTextNames;
		this.radioGroup = (RadioGroup) activity.findViewById(radioGroupId);
		radioGroup.setOnCheckedChangeListener(this);
		int num = this.radioGroup.getChildCount();
		for (int i = 0; i < num; i++) {
			RadioButton radioButton = (RadioButton) this.radioGroup
					.getChildAt(i);
			radioButton.setOnClickListener(this);
		}
		this.enableStr = enableStr;
		this.radioId = 0;
	}

	@Override
	public void getValue(Properties prop) {
		try {
			int selectId = radioGroup.getCheckedRadioButtonId();
			RadioButton radioButton = (RadioButton) activity
					.findViewById(selectId);
			if (radioButton != null) {
				String value = radioButton.getText().toString();
				prop.setProperty(name + "_content", value);
				prop.setProperty(this.name,
						radioGroup.indexOfChild(radioButton) + 1 + "");
				if (enableStr.equals(value)) {
					for (int i = 0; i < editTextIds.length; i++) {
						EditText editText = (EditText) this.activity
								.findViewById(editTextIds[i]);
						String edtValue = editText.getText().toString();
						if (edtValue != null) {
							prop.setProperty(editTextNames[i], edtValue);
						}
					}
				}
			} else {
				prop.setProperty(this.name + "_content", "");
				prop.setProperty(this.name, "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setValue(Properties prop) {
		try {
			String value = prop.getProperty(name + "_content");
			if (value != null && !value.equals("")) {
				int num = this.radioGroup.getChildCount();
				for (int i = 0; i < num; i++) {
					RadioButton radioButton = (RadioButton) this.radioGroup
							.getChildAt(i);
					if (value.equals(radioButton.getText().toString())) {
						this.radioGroup.check(radioButton.getId());
						radioId = radioButton.getId();
						break;
					}
				}
				if (enableStr.equals(value)) {
					for (int i = 0; i < editTextIds.length; i++) {
						EditText editText = (EditText) this.activity
								.findViewById(editTextIds[i]);
						editText.setEnabled(true);
						String edtValue = prop.getProperty(editTextNames[i]);
						if (edtValue != null) {
							editText.setText(edtValue);
						}
					}
				}
			} else {
				for (int i = 0; i < editTextIds.length; i++) {
					EditText editText = (EditText) this.activity
							.findViewById(editTextIds[i]);
					editText.setEnabled(false);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		int selectId = group.getCheckedRadioButtonId();
		RadioButton radioButton = (RadioButton) activity.findViewById(selectId);
		if (radioButton != null) {
			String str = radioButton.getText().toString();
			if (enableStr.equals(str)) {
				setEditTextEnabled(true);
			} else {
				setEditTextEnabled(false);
			}
		}
	}

	private void setEditTextEnabled(boolean bool) {
		try {
			for (int i = 0; i < editTextIds.length; i++) {
				EditText editText = (EditText) activity
						.findViewById(editTextIds[i]);
				if (!bool) {
					editText.setText(Constants.STR_NULL);
				}
				editText.setEnabled(bool);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		if (radioId == radioGroup.getCheckedRadioButtonId()) {
			radioGroup.clearCheck();
			setEditTextEnabled(false);
			radioId = 0;
		} else {
			radioId = radioGroup.getCheckedRadioButtonId();
		}
	}
}
